
#include "stdafx.h"
#include "Win32.Text.Parse.h"
#include "Win32.Number.h"

// -----------------------------------------------------------------------------

namespace Kif
{
namespace Win32
{
namespace Text
{

// -----------------------------------------------------------------------------

//template <typename T> T *Token<T>::NextToken()
//{
//   Parse::GetToken<T>(*this);
//   return(TokenBuffer);
//}  // end of NextToken

wchar_t *Token<wchar_t>::NextToken()
{
   Parse::GetToken(*this);
   return(TokenBuffer);
}  // end of NextToken

char *Token<char>::NextToken()
{
   Parse::GetToken(*this);
   return(TokenBuffer);
}  // end of NextToken

//template <typename T> double Token<T>::ToDouble()
//{
//   return 0.0;
//}  // end of ToDouble

double Token<wchar_t>::ToDouble()
{
   double outDouble = Kif::Win32::Number::ToDouble(TokenBuffer);
   return outDouble;
}  // end of ToDouble

//template <typename T> long Token<T>::ToLong()
//{
//   long outLong = Kif::Win32::Number::ToLong(TokenBuffer);
//   return outLong;
//}  // end of ToLong

long Token<wchar_t>::ToLong()
{
   long outLong = Kif::Win32::Number::ToLong(TokenBuffer);
   return outLong;
}  // end of ToLong

// -----------------------------------------------------------------------------

/// <summary>Given a buffer split it into two strings on the first found
/// equal char '='.</summary>
/// <param name="buffer">buffer to split</param>
/// <returns>a pointer to the ParseResults is returned if any, else
/// null</returns>
/// <remarks>caller must release the ParseResults using the delete
/// operator</remarks>
ParseResults *Parse::GetKeyValueStrings(TCHAR *buffer, size_t bufferLength)
{
   if (buffer == NULL)
      return(NULL);

   TCHAR *pc = buffer;

   // try to find the '=' char
   while (*pc && (*pc != '='))
      pc++;

   // if we didn't found the '=' char just return a null result
   if (*pc == NULL)
      return(NULL);

   size_t l1 = (pc - buffer);
   size_t l2 = bufferLength - l1 - 1;

   ParseResults *r = new ParseResults();
   if (r == NULL)
      return(NULL);

   r->Strings = new String[2];
   r->Strings[0].Copy(buffer,l1);
   r->Strings[1].Copy(buffer+l1+1,l2);

   r->Strings[0].Trim();
   r->Strings[1].Trim();

   r->Count = 2;

   return(r);
}  // end of GetKeyValueStrings

}  // end of Kif::Win32::Text namespace
}  // end of Kif::Win32 namespace
}  // end of Kif namespace
